// subPages/integral/orderDetails/orderDetails.js
const app = getApp()
var util =  require('../../../utils/util.js');
var json =  require('../../../data/json.js');
var flag = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logisticsName: '',
    logisticsNum: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(options.orderNum){
      that.setData({
        isShop: options.isShop?options.isShop:false,
        orderNum: options.orderNum
      })
      that.getData()
    }
    
  },

  //获取订单
  getData:function(){
    var that = this
    util.getAjax(app.globalData.apiUrl + '/get_jf_order_content',function(res){
      console.log(res)
      if(res.data.length!=0){
        that.setData({
          orderList: res.data[0]
        })
        that.setOrderData(that.data.orderList)
      }
    },{order_num: that.data.orderNum})
  },

  //设置订单
  setOrderData:function(orderList){
    var orderDetailsList = json.orderDetailsList;
    for(var i in orderDetailsList){
      switch (orderDetailsList[i].index) {
        case 1:
          orderDetailsList[i].value = orderList.order_num;
          break;
        case 2:
          orderDetailsList[i].value = util.setTime2(orderList.time)
          break;
        case 3:
          orderDetailsList[i].value = '积分兑换'
          break;
        case 4:
          orderDetailsList[i].value = util.getOrderStatus(orderList.status)
          break;
        case 5:
          if(orderList.address_order_shop){
            orderDetailsList[i].isShow = true;
            orderDetailsList[i].value = orderList.address_order_shop
          }else{
            orderDetailsList[i].isShow = false;
            orderDetailsList[i].value = ''
          }
          break;
        case 6:
          if(orderList.address_order_num){
            orderDetailsList[i].isShow = true;
            orderDetailsList[i].value = orderList.address_order_num
          }else{
            orderDetailsList[i].isShow = false;
            orderDetailsList[i].value = ''
          }
          break;
      }
    }
    this.setData({
      orderDetailsList: orderDetailsList,
    })
  },

  //查看商品详情
  shopDetails:function(e){
    var that = this
    wx.navigateTo({
      url: '../integralShopDetails/integralShopDetails?id=' + e.currentTarget.dataset.id
    })
  },


  //操作按钮
  operationBtn:function(e){
    var that = this
    var type = e.currentTarget.dataset.type    
    switch (type) {
      case '1':
        //提醒发货
        util.showToast('已提醒发货','success',2000)
        break;
      case '2':
        //确认收货
        util.getAjax(app.globalData.apiUrl + '/operation_community_jf_shop_order',function(res){
          console.log(res)
          if(res.data == 1){
            util.showToast('确认收货成功','success',2000)
            that.getData()
          }
        },{id: e.currentTarget.dataset.id,type: 2})
        break;
    }
  },

  //发货
  deliveryBtn:function(e){
    var that = this
    var id = e.currentTarget.dataset.id
    that.setData({
      orderId: id
    })
    var status = e.currentTarget.dataset.status
    that.deliveryAnim(status);
  },

  //获取物流公司
  logisticsNameInput:function(e){
    this.setData({
      logisticsName: e.detail.value
    })
  },

  //获取物流单号
  logisticsNumInput:function(e){
    this.setData({
      logisticsNum: e.detail.value
    })
  },

  //完成按钮
  OkDeliveryBtn:function(e){
    var that = this
    if(flag){
      return
    }
    flag = true
    if(!that.data.logisticsName || !that.data.logisticsNum){
      util.showToast('请填写完整物流信息','none',2000)
      flag = false
      return;
    }
    console.log(that.data.orderId)
    if(that.data.orderId){
      util.postAjax(`${app.globalData.apiUrl}/operation_community_jf_shop_order`,function(res){
        console.log(res)
        if(res.data == 1){
          util.showToast('发货成功','success',2000)
          var currentStatus = 'complete';
          that.deliveryAnim(currentStatus);
          that.getData()
          flag = false
        }else{
          util.showToast('发货失败，请重新点击','none',2000)
          flag = false
        }
      },{id: that.data.orderId,type: 1,address_order_shop: that.data.logisticsName,address_order_num: that.data.logisticsNum})
    }
  },
  
  //关闭按钮
  closeBtn:function(){
    var currentStatus = 'complete';
    this.deliveryAnim(currentStatus);
  },

  //动画部分
  deliveryAnim: function (currentStatus) {
    // 第1步：创建动画实例
    var animation = wx.createAnimation({
      duration: 200,  //动画时长
      timingFunction: "linear", //线性
      delay: 0  //0则不延迟
    });

    // 第2步：这个动画实例赋给当前的动画实例
    this.animation = animation;

    // 第3步：执行第一组动画：Y轴偏移240px后(盒子高度是400px)，停
    animation.translateY(350).step();

    // 第4步：导出动画对象赋给数据对象储存
    this.setData({
      deliveryAnim: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画
    setTimeout(function () {
      // 执行第二组动画：Y轴不偏移，停
      animation.translateY(0).step()
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象
      this.setData({
        deliveryAnim: animation
      })

      //完成
      if (currentStatus == "complete") {
        this.setData(
          {
            isDelivery: false,
            orderId: '',
            logisticsName: '',
            logisticsNum: '',
          }
        );
      }
    }.bind(this), 200)

    // 显示抽屉
    if (currentStatus == "open") {
      this.setData(
        {
          isDelivery: true,
        }
      );
    }
  },

  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
